package cn.wolfcde.luowowo.website.web.controller;

import cn.wolfcde.luowowo.website.annotation.LoginRequired;
import cn.wolfcde.luowowo.website.annotation.RedisParameter;
import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Strategy;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.TravelQueryObject;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisticsService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisticsService;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.service.ITravelCommentServiceService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.catalina.User;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.jws.WebParam;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("mine")
public class UserCenteController {
@Reference
private IUserInfoService userInfoService;
@Reference
private ITravelService travelService;
@Reference
private IDestinationService destinationService;
@Reference
private IStrategyService strategyService;
@Reference
private IStrategyStatisticsService strategyStatisticsService;
@Reference
private IStrategyDetailService strategyDetailService;
@Reference
private ITravelStatisticsService travelStatisticsService;
@Reference
private ITravelCommentServiceService travelCommentServiceService;
//---------------------------------------我的窝----------------------------------------------
    @RequestMapping("/homepage")
    public String homepage(@RedisParameter UserInfo userInfo, Model model,Long uid){
        //根据用户id获取用户信息
        UserInfo user=userInfoService.get(uid);
        //粉丝
       String fansStr=userInfoService.queryFansByUid(uid);
       if(fansStr!=null&&fansStr!=""){
           //将字符串切割成数组,String数组转为long数组
           String[] fansArr = fansStr.split(",");
           long[] fansList = (long[]) ConvertUtils.convert(fansArr,long.class);
           ArrayList<UserInfo> fans = new ArrayList<>();
           for (long fanId : fansList) {
               //根据用户id获取用户对象
               UserInfo fan = userInfoService.get(fanId);
               fans.add(fan);
               user.setFans(fans);
               user.setFansNum(fans.size());
           }
       }

        //关注
        String follwersList=userInfoService.queryFollwersByUid(uid);

       if(follwersList!=""&&follwersList!=null){
           //将字符串切割成数组,String数组转为long数组
           String[] follwersArr = follwersList.split(",");
           long[] strArrNum = (long[]) ConvertUtils.convert(follwersArr,long.class);
           ArrayList<UserInfo> follwers = new ArrayList<>();
           for (long follwersId : strArrNum) {
               //根据用户id获取用户对象
               UserInfo follwer = userInfoService.get(follwersId);
               follwers.add(follwer);
           }
           user.setFollwerNum(follwers.size());
           user.setFollwers(follwers);
       }


        //根据用户id查询用户下的所有游记
        List<Travel> travels = new ArrayList<>();
        List<Travel>list=travelService.queryTravlesByUid(uid);
        for (Travel travel : list) {
          Destination dest = travel.getDest();
            Destination destination = destinationService.selectById(dest.getId());
            travel.setDest(destination);
            travels.add(travel);
        }
        if(travels.size()>3){
            travels= travels.subList(0, 3);
        }


        //蜂蜜
        Integer honey=userInfoService.selectHoney(uid);
        user.setHoney(honey);

        //标志当前用户是否为个人中心的用户
        if(userInfo!=null){
            if(userInfo.getId()==uid){
                model.addAttribute("isMe",true);
            }else {
                model.addAttribute("isMe",false);
            }
        }


        model.addAttribute("user",user);

        model.addAttribute("travels",travels);
        model.addAttribute("num",travels.size());

        return "mine/homepage";
    }
    //---------------------------------------我的游记----------------------------------------------
    @RequestMapping("/mytravelnotes")
    public String mytravelnotes(@RedisParameter UserInfo userInfo, Model model, @ModelAttribute("qo")TravelQueryObject qo){
        //根据用户id获取用户信息
        UserInfo user=userInfoService.get(qo.getUid());

        PageInfo travels = travelService.query(qo);
        //回复数
        List<TravelComment> TravelComments = travelCommentServiceService.findCommentByUid(qo.getUid());
        model.addAttribute("commentNum",TravelComments.size());
        model.addAttribute("user",user);
        model.addAttribute("pageInfo",travels);
        //游记个数
        model.addAttribute("num",travels.getSize());
        //标志当前用户是否为个人中心的用户
        if(userInfo!=null){
            if(userInfo.getId()==qo.getUid()){
                model.addAttribute("isMe",true);
            }else {
                model.addAttribute("isMe",false);
            }
        }
        return "mine/mytravelnotes";
    }
    //---------------------------------------我的点评----------------------------------------------
    @RequestMapping("/review")
    public String review(Long uid, Model model,@RedisParameter UserInfo userInfo){
        //根据用户id获取用户信息
        UserInfo user=userInfoService.get(uid);
        model.addAttribute("user",user);
        //标志当前用户是否为个人中心的用户
        if(userInfo!=null){
            if(userInfo.getId()==uid){
                model.addAttribute("isMe",true);
            }else {
                model.addAttribute("isMe",false);
            }
        }
        return "mine/review";
    }
    //---------------------------------------我的收藏----------------------------------------------
    @RequestMapping("/travelcollection")
    public String travelcollection(Long uid, Model model,@RedisParameter UserInfo userInfo){
        //根据用户id获取用户信息
        UserInfo user=userInfoService.get(uid);

        model.addAttribute("user",user);
        //标志当前用户是否为个人中心的用户
        if(userInfo!=null){
            if(userInfo.getId()==uid){
                model.addAttribute("isMe",true);
            }else {
                model.addAttribute("isMe",false);
            }
        }
        return "mine/travelcollection";
    }
    @RequestMapping("/favorStrategy")//收藏攻略
    public String favorStrategy(Model model,@RedisParameter UserInfo userInfo){
        //查询所有的攻略
        ArrayList<StrategyDetail> list = new ArrayList<>();
        List<StrategyDetail> strategies = strategyDetailService.list();
        for (StrategyDetail strategyDetail : strategies) {
            Long id = strategyDetail.getId();

            boolean ret=strategyStatisticsService.isFavor(id,userInfo.getId());
            if(ret){

                list.add(strategyDetail);
            }
        }
        model.addAttribute("FavorStrategys",list);

        return "mine/favorstrategyTpl";
    }
    @RequestMapping("/favorTravel")
    public String favorTravel(Model model,@RedisParameter UserInfo userInfo){
        //查询所有的攻略
        ArrayList<Travel> list = new ArrayList<>();
        List<Travel> travels = travelService.list();
        for (Travel travel : travels) {
            Long id = travel.getId();
            boolean ret=travelStatisticsService.isFavorTravel(id,userInfo.getId());
            if(ret){
                list.add(travel);
            }
        }

        model.addAttribute("FavorTravels",list);
        return "mine/favortravelTpl";
    }


}
